package top.fengleifeng.question;

import top.fengleifeng.util.Constants;

import java.util.LinkedList;
import java.util.List;

/**
 * @program: leetcode-test
 * @description:
 * @author: fengleifeng
 * @create: 2020-09-29 11:11
 **/
public class Num145二叉树的后序遍历 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> list = new LinkedList<>();
            test(root, list);
            return list;
        }

        private void test(TreeNode root, List<Integer> list) {
            if (null != root) {
                if (null != root.left) {
                    test(root.left, list);
                }
                if (null != root.right) {
                    test(root.right, list);
                }
                list.add(root.val);
            }
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        treeNode.right = treeNode2;
        treeNode2.left = treeNode3;
        List<Integer> list = new Num145二叉树的后序遍历().new Solution().postorderTraversal(treeNode);
        Constants.pringLinkedList(list);
    }
}
